<template>
  <div class="app-container" >
    
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="500px">
      <el-form-item label="盘点单" prop="inventoryCheckNo" >
        <el-input
          v-model="queryParams.inventoryCheckNo"
          placeholder="请输入库存盘点单"
          
          @keyup.enter="handleQuery"
          readonly
        />
      </el-form-item>
      <br>
      <el-form-item label="盘点仓库" prop="itemName" label-width="500px">
        <el-select style="margin-left: 20px;" :span="6" readonly>
                <el-form-item label="仓库" prop="warehouseId">
                  <el-select @click="selectware" v-model="ware" placeholder="请选择仓库">
                    <el-option @click="seachware(dict.warehouseId)" v-for="dict in options"
                      :key="'key-' + dict.warehouseId" :label="dict.warehouseName"
                      :value="dict.warehouseId"></el-option>
                  </el-select>
                </el-form-item>
              </el-select>
              <el-select style="margin-left: 20px;" :span="6">
                <el-form-item label="库区" prop="">
                  <el-select v-model="area" placeholder="请选择库区">
                    <el-option @click="" v-for="dict in areas" :key="'key-' + dict.areaNo" :label="dict.areaName"
                      :value="dict.areaNo"></el-option>
                  </el-select>
                </el-form-item>
              </el-select>
              
      </el-form-item>
      
      <br>
      <el-form-item label="赢亏数" prop="inventoryCheckTotal" label-width="500px">
        <el-input-number style="width:100%" v-model="sum" :precision="2" :min="1" readonly></el-input-number>
      </el-form-item>
      <br>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="备注...100个字符以内" rows="4" maxlength="100" type="textarea"
        style="width: 700px;"      show-word-limit="show-word-limit"></el-input>
      </el-form-item>
      
      
    </el-form>
    <hr>
   
    <el-row :gutter="10" class="mb8">

        <el-col :span="22">

        物料明细

        </el-col>

        <el-col :span="1">

        

        </el-col>

</el-row>


    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料编号" align="center" prop="itemNo" />
      <el-table-column label="物料名" align="center" prop="itemName" />
      <el-table-column label="仓库" align="center" prop="warehouseName" />
      <el-table-column label="库区" align="center" prop="areaName" />
      <el-table-column label="账面库存" align="center" prop="quantity" />
      <el-table-column label="实际库存" align="center" prop="realQuantity" />
      <el-table-column label="变化量" align="center" >
        <template #default="scope">
          {{ scope.row.realQuantity - scope.row.quantity }}
        </template>
      </el-table-column>
      <el-table-column label="说明" align="center" prop="remark"/>


   
      
    </el-table>
    <!-- <el-button

          type="primary"

          plain

          @click="handleAdd"
          style="margin-left: 47%;"
          v-hasPermi="['wms:CheckDetail:add']"

        >添加物料</el-button> -->
      <br>
      <div style="margin-top: 10px"></div>
      <!-- <el-button label-width="500px" style="margin-left: 43%;">取消</el-button>
      <el-button label-width="500px" type="danger" >暂存</el-button>
      <el-button label-width="500px" type="primary">盘库结束</el-button> -->
      <el-button label-width="500px" @click="back()"  style="margin-left: 43%;" type="primary">返回</el-button>
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加物料 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
       <el-form :model="form" ref="detailRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品类型" prop="itemNo">
        <el-input
          v-model="queryParams.itemNo"
          placeholder="请选择分类"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快速筛选" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="编号，名称，id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>

       <!-- 表格 -->
       <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料名称" align="center" prop="itemName" />
      <el-table-column label="物料编号" align="center" prop="itemNo" />
    </el-table>

       <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Detail">
import { listCheckDetail,getCheckDetail, addCheckDetail, updateCheckDetail, delCheckDetail,DetailsList } from "@/api/wms/CheckDetail";
import { useRoute } from "vue-router";
const route=useRoute();



const {proxy } = getCurrentInstance();

const detailList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const sum = ref(1);
const inventoryCheckNo = ref(null);
// const inventoryCheckNo=ref(new Date().getTime());
console.log(route.query.inventoryCheckNo)
const data = reactive({
  form: {},
  queryParams: {
    inventoryCheckNo:route.query.inventoryCheckNo,
    pageNum: 1,
    pageSize: 10,
    inventoryCheckId: null,
    itemId: null,
    quantity: null,
    checkQuantity: null,
  },
  rules: {
    itemId: [
      { required: true, message: "物料不能为空", trigger: "blur" }
    ],
    delFlag: [
      { required: true, message: "删除标识不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);



/** 查询库存盘点单据详情列表 */
function getList() {
  loading.value = true;
  DetailsList(queryParams.value.inventoryCheckNo).then(response => {
    // console.log(queryParams.value.inventoryCheckNo);
    detailList.value = response.data.pdItem;
    console.log()
    console.log(response)
    total.value = response.total;
    loading.value = false;
  });
}

/** 返回按钮操作 */
function back() {
  proxy.$router.push({ name:"check",path: "/wms/check/index" });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    inventoryCheckId: null,
    itemId: null,
    quantity: null,
    checkQuantity: null,
    delFlag: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("detailRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加物料";
  
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCheckDetail(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改库存盘点单据详情";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["detailRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCheckDetail(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCheckDetail(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除库存盘点单据详情编号为"' + _ids + '"的数据项？').then(function() {
    return delCheckDetail(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/detail/export', {
    ...queryParams.value
  }, `detail_${new Date().getTime()}.xlsx`)
}
route.params.inventoryCheckNo = route.params&&route.params.inventoryCheckNo;
getList();
</script>
